package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.seekting.demo_lib.Demo;

/**
 * Created by seekting on 17-6-15.
 */

@Demo(title = "touch 事件研究", desc = "同一个布局，两个子view都收到事件")
public class TouchActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(this) {

            @Override
            public void setMotionEventSplittingEnabled(boolean split) {
                super.setMotionEventSplittingEnabled(split);
                Log.d("seekting", "TouchActivity.setMotionEventSplittingEnabled()", new NullPointerException());
            }

            @Override
            public boolean dispatchTouchEvent(MotionEvent ev) {
                boolean superResult =
                        super.dispatchTouchEvent(ev);


                int action = ev.getAction() & MotionEvent.ACTION_MASK;
                if (action == MotionEvent.ACTION_DOWN) {
                    Log.d("seekting", "TouchActivity.dispatchTouchEvent()linearLayout");
                }

                return superResult;
            }
        };
        linearLayout.setOrientation(LinearLayout.VERTICAL);
//        linearLayout.setMotionEventSplittingEnabled(false);
        setContentView(linearLayout);

        View v1 = new Button(this) {
            @Override
            public boolean onTouchEvent(MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.d("seekting", "TouchActivity.onTouchEvent()v1");
                }
                return super.onTouchEvent(event);
            }
        };
        v1.setBackgroundResource(android.R.mipmap.sym_def_app_icon);
        v1.setOnClickListener(this);

        View v2 = new ImageView(this) {
            @Override
            public boolean onTouchEvent(MotionEvent event) {


                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Log.d("seekting", "TouchActivity.onTouchEvent()v2");
                }
                return super.onTouchEvent(event);
            }
        };
        v2.setBackgroundResource(android.R.mipmap.sym_def_app_icon);

        v2.setOnClickListener(this);

        linearLayout.addView(v1);
        linearLayout.addView(v2);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) v2.getLayoutParams();
        layoutParams.leftMargin = layoutParams.topMargin = layoutParams.bottomMargin = layoutParams.rightMargin = 100;


        layoutParams = (LinearLayout.LayoutParams) v1.getLayoutParams();
        layoutParams.leftMargin = layoutParams.topMargin = layoutParams.bottomMargin = layoutParams.rightMargin = 100;

        boolean splite = linearLayout.isMotionEventSplittingEnabled();
        Log.d("seekting", "TouchActivity.onCreate()" + splite);

    }

    @Override
    public void onClick(View v) {

    }
}
